home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWOSMisc / FWTime.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  14.4 KB  |  451 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWTime.h
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #if !defined(FWTIME_H) && !defined(__ODFRC__)
  11. #define FWTIME_H
  12.  
  13. #ifndef FWSTDDEF_H
  14. #include "FWStdDef.h"
  15. #endif
  16.  
  17. #if defined(FW_BUILD_MAC) && !defined(__TIME_H__)
  18. #include <Time.h>
  19. #endif
  20.  
  21. #if defined(FW_BUILD_MAC) && !defined(__OSUTILS__)
  22. #include <OSUtils.h>
  23. #endif
  24.  
  25. #if defined(FW_BUILD_WIN) && !defined(__TIME_H__)
  26. #include <Time.h>
  27. #endif
  28.  
  29. //========================================================================================
  30. //    Forward Class Declarations
  31. //========================================================================================
  32. class FW_CTimeSpan;
  33. class FW_CString;
  34.  
  35. //========================================================================================
  36. //    FW_CTime
  37. //========================================================================================
  38.  
  39. class FW_CTime
  40. {
  41. public:
  42.     enum  DateForm { kShortDate, kLongDate, kAbbreviatedDate};
  43.     
  44.     FW_CTime();
  45.     FW_CTime(time_t theTime);
  46.     FW_CTime(const FW_CTime& other);
  47.     FW_CTime(short year, short month, short day, short hour, short minute, short second);
  48.     ~FW_CTime();
  49.     
  50.     static FW_CTime GetCurrentTime();    // Create a new time initialized to the current time
  51.     
  52.     // Accessors
  53.     time_t GetTime() const;
  54.     
  55.     short GetSecond() const;
  56.     short GetMinute() const;
  57.     short GetHour() const;
  58.     short GetDayOfWeek() const;
  59.     short GetDayOfMonth() const;
  60.     short GetDayOfYear() const;
  61.     short GetMonth() const;
  62.     short GetYear() const;
  63.     short IsDaylightSavings() const;
  64.  
  65. #ifdef FW_BUILD_MAC
  66.     static unsigned long MacConvertODTimeToMacTime(ODTime t);
  67.     static ODTime MacConvertMacTimeToODTime(unsigned long t);
  68. #endif
  69.  
  70.     void GetTimeString(FW_CString& timeString, FW_Boolean displaySeconds) const;
  71.     void GetDateString(FW_CString& dateString, DateForm form) const;
  72.     
  73.     const FW_CTime&    operator=(const FW_CTime& other);
  74.     const FW_CTime& operator=(time_t time);
  75.     
  76.     FW_CTime operator-(FW_CTimeSpan timeSpan) const;
  77.     FW_CTimeSpan operator-(FW_CTime time) const;
  78.     FW_CTime operator+(FW_CTimeSpan timeSpan) const;
  79.     const FW_CTime& operator-=(FW_CTimeSpan timeSpan);
  80.     const FW_CTime& operator+=(FW_CTimeSpan timeSpan);
  81.  
  82.     FW_Boolean operator==(FW_CTime time) const;
  83.     FW_Boolean operator!=(FW_CTime time) const;
  84.     FW_Boolean operator<(FW_CTime time) const;
  85.     FW_Boolean operator>(FW_CTime time) const;
  86.     FW_Boolean operator<=(FW_CTime time) const;
  87.     FW_Boolean operator>=(FW_CTime time) const;
  88.     
  89. private:
  90.     time_t fTime;
  91. };
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // FW_CTime constructor
  95. //----------------------------------------------------------------------------------------
  96.  
  97. inline FW_CTime::FW_CTime(time_t theTime) :
  98.     fTime(theTime)
  99. {
  100. }
  101.  
  102. //----------------------------------------------------------------------------------------
  103. // FW_CTime constructor
  104. //----------------------------------------------------------------------------------------
  105.  
  106. inline FW_CTime::FW_CTime(const FW_CTime& other) :
  107.     fTime(other.fTime)
  108. {
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // FW_CTime destructor
  113. //----------------------------------------------------------------------------------------
  114.  
  115. inline FW_CTime::~FW_CTime()
  116. {
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // FW_CTime::GetTime
  121. //----------------------------------------------------------------------------------------
  122.  
  123. inline time_t FW_CTime::GetTime() const
  124. {
  125.     return fTime;
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. //    FW_CTime::operator =
  130. //----------------------------------------------------------------------------------------
  131.  
  132. inline const FW_CTime& FW_CTime::operator=(const FW_CTime& other)
  133. {
  134.     fTime = other.GetTime();
  135.     return *this;
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. //    FW_CTime::operator =
  140. //----------------------------------------------------------------------------------------
  141.  
  142. inline const FW_CTime& FW_CTime::operator=(time_t time)
  143. {
  144.     fTime = time;
  145.     return *this;
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. //    FW_CTime::operator ==
  150. //----------------------------------------------------------------------------------------
  151.  
  152. inline FW_Boolean FW_CTime::operator==(FW_CTime time) const
  153. {
  154.     return fTime == time.GetTime();
  155. }
  156.  
  157. //----------------------------------------------------------------------------------------
  158. //    FW_CTime::operator !=
  159. //----------------------------------------------------------------------------------------
  160.  
  161. inline FW_Boolean FW_CTime::operator!=(FW_CTime time) const
  162. {
  163.     return fTime != time.GetTime();
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. //    FW_CTime::operator <
  168. //----------------------------------------------------------------------------------------
  169.  
  170. inline FW_Boolean FW_CTime::operator<(FW_CTime time) const
  171. {
  172.     return fTime < time.GetTime();
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    FW_CTime::operator >
  177. //----------------------------------------------------------------------------------------
  178.  
  179. inline FW_Boolean FW_CTime::operator>(FW_CTime time) const
  180. {
  181.     return fTime > time.GetTime();
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. //    FW_CTime::operator <=
  186. //----------------------------------------------------------------------------------------
  187.  
  188. inline FW_Boolean FW_CTime::operator<=(FW_CTime time) const
  189. {
  190.     return fTime <= time.GetTime();
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. //    FW_CTime::operator >=
  195. //----------------------------------------------------------------------------------------
  196.  
  197. inline FW_Boolean FW_CTime::operator>=(FW_CTime time) const
  198. {
  199.     return fTime >= time.GetTime();
  200. }
  201.  
  202.  
  203. class FW_CTimeSpan
  204. {
  205. public:
  206.     FW_CTimeSpan();
  207.     FW_CTimeSpan(time_t theTimeSpan);
  208.     FW_CTimeSpan(const FW_CTimeSpan& other);
  209.     FW_CTimeSpan(long days, short hours, short minutes, short seconds);
  210.     ~FW_CTimeSpan();
  211.     
  212.     short    GetSeconds() const;
  213.     short    GetMinutes() const;
  214.     short    GetHours() const;
  215.     long    GetDays() const;
  216.     
  217.     long    GetTotalSeconds() const;
  218.     long    GetTotalMinutes() const;
  219.     long    GetTotalHours() const;
  220.     
  221.     const FW_CTimeSpan&    operator=(const FW_CTimeSpan& source);
  222.     const FW_CTimeSpan& operator-=(FW_CTimeSpan timeSpan);
  223.     const FW_CTimeSpan& operator+=(FW_CTimeSpan timeSpan);
  224.  
  225.     FW_CTimeSpan operator-(FW_CTimeSpan timeSpan) const;
  226.     FW_CTimeSpan operator+(FW_CTimeSpan timeSpan) const;
  227.     
  228.     FW_Boolean operator==(FW_CTimeSpan timeSpan) const;
  229.     FW_Boolean operator!=(FW_CTimeSpan timeSpan) const;
  230.     FW_Boolean operator<(FW_CTimeSpan timeSpan) const;
  231.     FW_Boolean operator>(FW_CTimeSpan timeSpan) const;
  232.     FW_Boolean operator<=(FW_CTimeSpan timeSpan) const;
  233.     FW_Boolean operator>=(FW_CTimeSpan timeSpan) const;
  234.  
  235. private:
  236.     long fTotalSeconds;
  237. };
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // FW_CTimeSpan constructor
  241. //----------------------------------------------------------------------------------------
  242.  
  243. inline FW_CTimeSpan::FW_CTimeSpan()
  244. {
  245.     fTotalSeconds = 0;
  246. }
  247.  
  248. //----------------------------------------------------------------------------------------
  249. // FW_CTimeSpan constructor
  250. //----------------------------------------------------------------------------------------
  251.  
  252. inline FW_CTimeSpan::FW_CTimeSpan(time_t theTimeSpan)
  253. {
  254.     fTotalSeconds = theTimeSpan;
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // FW_CTimeSpan constructor
  259. //----------------------------------------------------------------------------------------
  260.  
  261. inline FW_CTimeSpan::FW_CTimeSpan(const FW_CTimeSpan& other)
  262. {
  263.     fTotalSeconds = other.fTotalSeconds;
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // FW_CTimeSpan constructor
  268. //----------------------------------------------------------------------------------------
  269.  
  270. inline FW_CTimeSpan::FW_CTimeSpan(long days, short hours, short minutes, short seconds)
  271. {
  272. //    fTotalSeconds = (days * 86400L) + (hours * 3600) + (minutes * 60) + seconds;
  273.     fTotalSeconds = seconds + 60L*(minutes + 60L*(hours + 24*days));
  274. }
  275.  
  276. //----------------------------------------------------------------------------------------
  277. // FW_CTimeSpan destructor
  278. //----------------------------------------------------------------------------------------
  279.  
  280. inline FW_CTimeSpan::~FW_CTimeSpan()
  281. {
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // FW_CTimeSpan::GetSeconds
  286. //----------------------------------------------------------------------------------------
  287.  
  288. inline short FW_CTimeSpan::GetSeconds() const
  289. {
  290.     return fTotalSeconds % 60;
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. // FW_CTimeSpan::GetMinutes
  295. //----------------------------------------------------------------------------------------
  296.  
  297. inline short FW_CTimeSpan::GetMinutes() const
  298. {
  299.     return (fTotalSeconds % 3600) / 60;
  300. }
  301.  
  302. //----------------------------------------------------------------------------------------
  303. // FW_CTimeSpan::GetHours
  304. //----------------------------------------------------------------------------------------
  305.  
  306. inline short FW_CTimeSpan::GetHours() const
  307. {
  308.     return (fTotalSeconds % 86400L) / 3600;
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. // FW_CTimeSpan::GetDays
  313. //----------------------------------------------------------------------------------------
  314.  
  315. inline long FW_CTimeSpan::GetDays() const
  316. {
  317.     return fTotalSeconds / 86400L;    // 60 seconds * 60 minutes * 24 hours
  318. }
  319.  
  320.  
  321. //----------------------------------------------------------------------------------------
  322. // FW_CTimeSpan::GetTotalSeconds
  323. //----------------------------------------------------------------------------------------
  324.  
  325. inline long FW_CTimeSpan::GetTotalSeconds() const
  326. {
  327.     return fTotalSeconds;
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // FW_CTimeSpan::GetTotalMinutes
  332. //----------------------------------------------------------------------------------------
  333.  
  334. inline long FW_CTimeSpan::GetTotalMinutes() const
  335. {
  336.     return fTotalSeconds / 60;
  337. }
  338.  
  339. //----------------------------------------------------------------------------------------
  340. // FW_CTimeSpan::GetTotalHours
  341. //----------------------------------------------------------------------------------------
  342.  
  343. inline long FW_CTimeSpan::GetTotalHours() const
  344. {
  345.     return fTotalSeconds / 3600;
  346. }
  347.  
  348. //----------------------------------------------------------------------------------------
  349. //    FW_CTimeSpan::operator =
  350. //----------------------------------------------------------------------------------------
  351.  
  352. inline const FW_CTimeSpan& FW_CTimeSpan::operator=(const FW_CTimeSpan& source)
  353. {
  354.     fTotalSeconds = source.GetTotalSeconds();
  355.     return *this;
  356. }
  357.  
  358. //----------------------------------------------------------------------------------------
  359. //    FW_CTimeSpan::operator -=
  360. //----------------------------------------------------------------------------------------
  361.  
  362. inline const FW_CTimeSpan& FW_CTimeSpan::operator-=(FW_CTimeSpan source)
  363. {
  364.     fTotalSeconds -= source.GetTotalSeconds();
  365.     return *this;
  366. }
  367.  
  368. //----------------------------------------------------------------------------------------
  369. //    FW_CTimeSpan::operator +=
  370. //----------------------------------------------------------------------------------------
  371.  
  372. inline const FW_CTimeSpan& FW_CTimeSpan::operator+=(FW_CTimeSpan source)
  373. {
  374.     fTotalSeconds += source.GetTotalSeconds();
  375.     return *this;
  376. }
  377.  
  378. //----------------------------------------------------------------------------------------
  379. //    FW_CTimeSpan::operator -
  380. //----------------------------------------------------------------------------------------
  381.  
  382. inline FW_CTimeSpan FW_CTimeSpan::operator-(FW_CTimeSpan timeSpan) const
  383. {
  384.     return FW_CTimeSpan((time_t)(GetTotalSeconds() - timeSpan.GetTotalSeconds()));
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388. //    FW_CTimeSpan::operator +
  389. //----------------------------------------------------------------------------------------
  390.  
  391. inline FW_CTimeSpan FW_CTimeSpan::operator+(FW_CTimeSpan timeSpan) const
  392. {
  393.     return FW_CTimeSpan((time_t)(GetTotalSeconds() + timeSpan.GetTotalSeconds()));
  394. }
  395.  
  396. //----------------------------------------------------------------------------------------
  397. //    FW_CTimeSpan::operator ==
  398. //----------------------------------------------------------------------------------------
  399.  
  400. inline FW_Boolean FW_CTimeSpan::operator==(FW_CTimeSpan timeSpan) const
  401. {
  402.     return fTotalSeconds == timeSpan.GetTotalSeconds();
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. //    FW_CTimeSpan::operator !=
  407. //----------------------------------------------------------------------------------------
  408.  
  409. inline FW_Boolean FW_CTimeSpan::operator!=(FW_CTimeSpan timeSpan) const
  410. {
  411.     return fTotalSeconds != timeSpan.GetTotalSeconds();
  412. }
  413.  
  414. //----------------------------------------------------------------------------------------
  415. //    FW_CTimeSpan::operator <
  416. //----------------------------------------------------------------------------------------
  417.  
  418. inline FW_Boolean FW_CTimeSpan::operator<(FW_CTimeSpan timeSpan) const
  419. {
  420.     return fTotalSeconds < timeSpan.GetTotalSeconds();
  421. }
  422.  
  423. //----------------------------------------------------------------------------------------
  424. //    FW_CTimeSpan::operator >
  425. //----------------------------------------------------------------------------------------
  426.  
  427. inline FW_Boolean FW_CTimeSpan::operator>(FW_CTimeSpan timeSpan) const
  428. {
  429.     return fTotalSeconds > timeSpan.GetTotalSeconds();
  430. }
  431.  
  432. //----------------------------------------------------------------------------------------
  433. //    FW_CTimeSpan::operator <=
  434. //----------------------------------------------------------------------------------------
  435.  
  436. inline FW_Boolean FW_CTimeSpan::operator<=(FW_CTimeSpan timeSpan) const
  437. {
  438.     return fTotalSeconds <= timeSpan.GetTotalSeconds();
  439. }
  440.  
  441. //----------------------------------------------------------------------------------------
  442. //    FW_CTimeSpan::operator >=
  443. //----------------------------------------------------------------------------------------
  444.  
  445. inline FW_Boolean FW_CTimeSpan::operator>=(FW_CTimeSpan timeSpan) const
  446. {
  447.     return fTotalSeconds >= timeSpan.GetTotalSeconds();
  448. }
  449.  
  450. #endif
  451.